<?php

class Antz_Input
{
    protected $post;
    protected $get;
    protected $files;
    protected $inputFilter;
    public $strlen;
    public $allowTags = array();
    public $allowAtts = array();

    public function __construct(){
        $this->post =& $_POST;
        $this->get =& $_GET;
        $this->files =& $_FILES;
        $this->strlen = 255;
        // please contact me if I have overlooked your favourite tag or attribute!
        $this->setAllowAtts(array(
			'class',
			'id',
			'src',
			'alt',
			'title',
			'style',
			'align',
			'href'
			)
		);
        $this->setAllowTags(array(
			'h1',
			'h2',
			'h3',
			'h4',
			'h5',
			'h6',
			'div',
			'p',
			'li',
			'ol',
			'ul',
			'b',
			'a',
			'strong',
			'em',
			'i',
			'u',
			'strike',
			'img',
			'blockquote',
			'dt',
			'dl',
			'dd',
			'code'
			)
		);
    }

    public function setAllowTags($tags){
        $this->allowTags = $tags;
        $this->inputFilter = Antz::newObject('ThirdParty_InputFilter', array($this->allowTags, $this->allowAtts));
    }

    public function setAllowAtts($atts){
        $this->allowAtts = $atts;
        $this->inputFilter = Antz::newObject('ThirdParty_InputFilter', array($this->allowTags, $this->allowAtts));
    }

    public function getAllowTags(){
        return $this->allowTags;
    }
    
    public function getAllowAtts(){
        return $this->allowAtts;
    }

    public function getPostValue($key, $strlen='', $retval=false){
        if($strlen == '') $strlen = $this->strlen;
        if(array_key_exists($key, $this->post)){
            if(!is_array($this->post[$key])){
                $retval = $this->inputFilter->process($this->post[$key]);
                $retval = substr($retval, 0, $strlen);
            }else{
                $retval = $this->post[$key];
                foreach($retval as $k=>$v){
                    $v = $this->inputFilter->process($v);
                    $retval[$k] = substr($v, 0, $strlen);
                };
                $retval = $this->post[$key];
            };
        };
        return $retval;
    }

    public function getFilesValue($key, $strlen='', $retval=false){
        if($strlen == '') $strlen = $this->strlen;
        if(array_key_exists($key, $this->files)){
            if(!is_array($this->files[$key])) $retval = substr($this->files[$key], 0, $strlen);
            else{
                foreach($this->files[$key] as $k=>$v){
                    $this->files[$key][$k] = substr($v, 0, $strlen);
                };
                $retval = $this->files[$key];
            };
        };
        return $retval;
    }

    public function getGetValue($key, $strlen='', $retval=false){
        if($strlen == '') $strlen = $this->strlen;
        if(array_key_exists($key, $this->get)){
            if(!is_array($this->get[$key])) $retval = substr($this->get[$key], 0, $strlen);
            else{
                foreach($this->get[$key] as $k=>$v){
                    $this->get[$key][$k] = substr($v, 0, $strlen);
                };
                $retval = $this->get[$key];
            };
        };
        return $retval;
    }
}


